package com.ma4567.remotecontroller.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.UserManager.User;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;

class NginxSetting{
    public String NginxPath;

    public NginxSetting(){
        String SettingSave = System.getProperty("user.dir") + "\\NginxSetting.json";
        java.io.File SetFile = new File(SettingSave);
        if(!SetFile.exists()) {
            this.NginxPath = "D:\\IDE\\nginx";
            WriteFile();
        }

        ReadFile();
    }

    public void WriteFile() {
        String filePath = System.getProperty("user.dir") + "\\NginxSetting.json";

        try
        {
            Files.deleteIfExists(Paths.get(filePath));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        String JsonStr = JSONObject.toJSONString(this, SerializerFeature.PrettyFormat);

        try
        {
            Files.createFile(Paths.get(filePath));
            Files.write(Paths.get(filePath), "NginxPath D:\\IDE\\nginx".getBytes());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void ReadFile(){
        String filePath = System.getProperty("user.dir") + "\\NginxSetting.json";
        try{
            String UserJson = Files.readString(Paths.get(filePath));
            this.NginxPath = UserJson.split(" ")[1];
        }
        catch(Exception e){
            e.printStackTrace();
            WriteFile();
        }
    }
}

public class NginxController implements Runnable {
    public static NginxSetting set;
    public static boolean NginxEnabled = false;
    public static Process process = null;

    public NginxController() {
    }

    public static void StartNginx(){
        set = new NginxSetting();
        try {
            if(process == null) {
                String myExe = "cmd /c start nginx.exe ";

                File dir = new File(set.NginxPath);
                String[] str = new String[]{};

                process = Runtime.getRuntime().exec(myExe, str, dir);
                process.waitFor();

                BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
                BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                String s;

                while ((s = stdInput.readLine()) != null) System.out.println(s);
                while ((s = stdError.readLine()) != null) System.err.println(s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void StopNginx(){
        try {
            String myExe = "cmd /c nginx.exe -s quit ";

            File dir = new File(set.NginxPath);
            String[] str = new String[] {};

            Process process = Runtime.getRuntime().exec(myExe, str, dir);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;

            while ((s = stdInput.readLine()) != null) System.out.println(s);
            while ((s = stdError.readLine()) != null) System.err.println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void run(){
        StartNginx();
    }
}
